(function () {
    var FormElement = function () {

    }
    FormElement.prototype = {
        init: function () {
            var $this = this;
            $this.check();
            $this.select();
        },
        check: function () {
            $(":checkbox").each(function () {
                var title = $(this).attr("title");
                var funcType = $(this).attr("tag-index");
                var div = $("<div class=\"layui-unselect layui-form-checkbox\" func-type=" + funcType + " lay-skin=\"primary\">"
                    + "<span>" + title + "</span><i class=\"layui-icon\"></i></div>");
                $(div).click(function () {
                    var che = $(this).prev().prop("checked");
                    $(this).prev().prop("checked", !che);
                    if (!che) {
                        $(this).addClass("layui-form-checked");
                    } else {
                        $(this).removeClass("layui-form-checked");
                    }
                });
                $(this).after(div);
            });
        },
        select: function () {
            $("select").each(function (i, o) {
                var _this = $(this);
                var $container = $("<div target='selectContainer' tabIndex='" + i + "'  class='layui-unselect layui-form-select'></div>");
                var $showElement = $("<div class='layui-select-title'><input type='text' readonly='readonly' placeholder='' value='' class='layui-input layui-unselect'><i class='layui-edge' ></i></div>");
                $container.append($showElement);
                var $dl = $("<dl class='layui-anim layui-anim-upbit'></dl>");
                $("option", o).each(function () {
                    var selected = $(this).prop("selected");
                    var val = $(this).val();
                    var text = $(this).text();
                    var $dd = $("<dd lay-value='" + val + "'>" + text + "</dd>");
                    if (val === "") {
                        $dd = $("<dd lay-value class='layui-select-tips'>" + (text || "请选择") + "</dd>");
                    }
                    if (selected) {
                        $dd.addClass("layui-this");
                        $("input:first", $showElement).val(text);
                    }

                    $dd.click(function () {
                        var $ddValue = $(this).attr("lay-value");
                        var $ddText = $(this).text();
                        $(this).parent().find(".layui-this").removeClass("layui-this");
                        if ($ddValue !== "")
                            $(this).addClass("layui-this");
                        $("input:first", $showElement).val($ddText);
                        _this.val($ddValue);
                    });
                    $dl.append($dd);
                });
                $container.append($dl);
                $container.click(function () {
                    var hasClass = $container.hasClass("layui-form-selected");
                    if (hasClass) {
                        $container.removeClass("layui-form-selected");
                    } else {
                        $container.addClass("layui-form-selected");
                        $(this).attr("open", "true");
                    }
                    $(document).one('mouseup', function (e) {
                        close($container, e);
                    });
                });

                $(this).after($container);
            });
        }
    };
    var fromElement = new FormElement();
    fromElement.init();

    function close(o, e) {
        var t = e.target;
         t = $(t).parents(".layui-form-select")[0];
        if (t === undefined && o.attr("tabIndex") !== $(t).attr("tabIndex")) {
            $(o).removeClass("layui-form-selected");
        }
    }
}(jQuery));
